package Blood.SqlParse;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class Main {

    public static void main(String[] args) {
       FileSpider fs = new FileSpider();
       fs.spiderSqlFiles("F:/yh/yc_data/workflow/biz");
       LineageUtils lu = new LineageUtils();
       fs.getHiveSqlFiles().forEach(x ->{
           try {
               lu.parseSql(x.split(",")[0]);
           } catch (Exception e) {
               System.out.println(x);
               e.printStackTrace();
           }
       });

       new Thread(new Runnable() {
           @Override
           public void run() {
              List<TableNode> nodes = new ArrayList<>();
              Connection connection = null ;
              PreparedStatement preparedStmt = null ;
              try {
                  connection = DB.getConnection();
                  preparedStmt = DB.getPreparedStmt("insert into blood.blood_node(execute_platform,script_path,source_table,table_name) " +
                                          "values(?,?,?,?)", connection);
              } catch (ClassNotFoundException e) {
                  e.printStackTrace();
              } catch (SQLException e) {
                  e.printStackTrace();
              }

              try {
                   while (lu.getBlockingQueue().size()>0){

                      nodes.add(lu.getBlockingQueue().poll(1,TimeUnit.SECONDS));
                      if(nodes.size() > 100) {
                        DB.insertBatch(preparedStmt,nodes);
                        nodes.clear();
                      }

                   }

                 if(nodes.size()>0){
                   DB.insertBatch(preparedStmt,nodes);
                 }

              } catch (InterruptedException e) {
                      e.printStackTrace();
              }finally {
                DB.close(connection);
                DB.close(preparedStmt);
              }
           }
       }).start();
    }

}
